﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Marbles.Core.Connectors;

namespace Marbles.Core.Content.Profiles
{
    public class ProfileResponseCollection : IEnumerable<ProfileResponse>
    {

        public ProfileResponse this[int index] { get { return items[index]; } set { items[index] = value; } }
        public string SiteId { get { return _siteid; } }
        public int Count
        {
            get { return items.Count; }
        }

        public string UserId { get { return _userid; } }
        public ProfileQuestionCollection Questions { get { return _questions; } }

        private string _userid = string.Empty;
        private List<ProfileResponse> items = null;
        private ProfileQuestionCollection _questions = null;
        private string _siteid = string.Empty;

        public ProfileResponseCollection(string SiteId, string UserId)
        {
            items = new List<ProfileResponse>();
            _userid = UserId;
            _questions = new ProfileQuestionCollection(SiteId);
            _siteid = SiteId;

            items.Clear();

            foreach (ProfileQuestion question in _questions)
            {
                items.Add(new ProfileResponse(UserId, question));
            }

        }

        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM userprofiles WHERE userid = @userid AND siteid = @siteid");
            cmd.AddParameter("@userid", _userid);
            cmd.AddParameter("siteid", _siteid);
            dbc.ExecuteCommand(cmd);
        }

        public void Update()
        {
            Delete();
            foreach (ProfileResponse response in items)
            {
                response.Update();
            }
        }

        public IEnumerator<ProfileResponse> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}